package design_pattern;

import java.util.ArrayList;
import java.util.List;

//观察者模式
class  ObserverPatttern{
    public static void main(String[] args) {
        Debit zhangsan = new Zhangsan();
        zhangsan.borrow(new WangWu());//借款方向许多贷款方借钱

        //state改变
        zhangsan.notifyCredits();//通知我有钱了，可以还钱
    }

}
//借款方
public interface Debit {
    ///借
    void borrow(Credit credit);
    //通知
    void notifyCredits();
}
//贷款方
interface Credit{
    void takeMoney();
}
class WangWu implements Credit{

    @Override
    public void takeMoney() {

    }
}
class Zhangsan implements Debit{
    private List<Credit> allCredits = new ArrayList<>();
    private Integer state = 0; //1表示有钱

    @Override
    public void borrow(Credit credit) {
        allCredits.add(credit);
    }

    @Override
    public void notifyCredits() {
        allCredits.forEach(credit -> credit.takeMoney());
    }
}